<template>
  <div class="el-dialog-div" ref="formBox">
    <el-form ref="form" :model="form" label-width="130px" :rules="rules">

      <el-form-item label="父商城" prop="parentId">
        <el-select v-model="form.parentId" placeholder="" clearable class="input-W" @change="selectSecCode">
          <el-option v-for="dict in this.parentShopName" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="类别名称" prop="categoryName">
        <el-input v-model="form.categoryName" class="input-W" placeholder="类别名称" />
      </el-form-item>


      <el-form-item label="活动标记" prop="marketFlag">
        <el-select v-model="form.marketFlag" placeholder="" clearable class="input-W" @change="selectSecCode">
          <el-option v-for="dict in this.marketFlag" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否显示" prop="showFlag">
        <el-select v-model="form.showFlag" placeholder="" clearable class="input-W">
          <el-option v-for="dict in this.showFlag" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item v-if="form.marketFlag=='2'" label="第三方地址" prop="imgLink">
        <el-input v-model="form.imgLink" class="input-W" placeholder="第三方地址" />
      </el-form-item>

      <el-form-item label="排序" prop="seq">
        <el-input-number v-model="form.seq" controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="缩略图" prop="imgMin">
        <image-upload :valueData="form.imgMin" params="imgMin" @uploaded="uploaded"></image-upload>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>

export default {
  name: 'formView',
  props: {
    form: {
      type: Object,
    },
    marketFlag: {
      type: Array,
    },
    showFlag: {
      type: Array,
    },
    parentShopName: {
      type: Array,
    }
  },

  data() {
    const validtext = (rule, value, callback) => {
      let textValue = this.$refs.editor.currentValue;
      if (!textValue) {
        callback(new Error('请输入详情内容'))
      } else {
        callback()
      }
    };
    const imgValidtext = (rule, value, callback) => {
      let textValue = this.form.imgMin;
      if (textValue && textValue[1] != ']') {
        callback()
      } else {
        callback(new Error('请上传大图'))
      }
    }
    return {
      list: [],

      rules: {
        parentId: [{ required: true, message: "父商城", trigger: "blur" }],
        categoryName: [{ required: true, message: "分类名称", trigger: "blur" }],
        seq: [{ required: true, message: "购买赠送类型必填", trigger: "blur" }],
        marketFlag: [{ required: true, message: "活动标记", trigger: "blur" }],
        showFlag: [{ required: true, message: "是否显示", trigger: "blur" }],
        imgLink: [{ required: true, message: "地址必填", trigger: "blur" }],
        imgMin: [{ required: true, message: "大图必填", trigger: ["blur", "change"], validator: imgValidtext }],  //大图
      },
    }
  },
  mounted() {
  },
  updated() {
  },
  methods: {

    selectSecCode(a) {

      if (a == 2) {
        this.rules.imgLink.required = false;
      } else {
        // this.form.imgLink = null;

        this.rules.imgLink.required = true;
      }
    },

    uploaded(params, dict) {
      this.form[params] = JSON.stringify(dict);
    },
  }
}
</script>

<style lang="scss" scoped>
.el-dialog-div {
  height: 70vh;
  overflow: auto;
  padding-right: 30px;
}

.input-W {
  width: 100%;
}
</style>
